package com.example.administrator.android2019.ch2Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.android2019.R;

public class ComputerActivity extends AppCompatActivity {
    EditText ed;
     TextView tv;
    Button btn_back, btn_clear, btn_7, btn_8, btn_9, btn_div;
    Button btn_4, btn_5, btn_6, btn_mul, btn_1, btn_2, btn_3, btn_sub;
    Button btn_0, btn_point, btn_equal, btn_add;
    /* 操作数   操作符  */
    double n1,n2;
    String op;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_computer);
        init();
        View.OnClickListener click=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 switch (v.getId()){
                     case R.id.ch2_calc_btn_0:
                     case R.id.ch2_calc_btn_1:
                     case R.id.ch2_calc_btn_2:
                     case R.id.ch2_calc_btn_3:
                     case R.id.ch2_calc_btn_4:
                     case R.id.ch2_calc_btn_5:
                     case R.id.ch2_calc_btn_6:
                     case R.id.ch2_calc_btn_7:
                     case R.id.ch2_calc_btn_8:
                     case R.id.ch2_calc_btn_9:
                    number(v);
                    break;
                     case R.id.ch2_calc_btn_point:
                         point();
                         break;
                     case R.id.ch2_calc_btn_add:
                     case R.id.ch2_calc_btn_sub:
                     case R.id.ch2_calc_btn_mul:
                     case R.id.ch2_calc_btn_div:
                          operator(v);
                          break;
                     case R.id.ch2_calc_btn_equal:
                         equal();
                     case R.id.ch2_calc_btn_back:
                         back();
                         break;
                     case R.id.ch2_calc_btn_clear:
                         clear();
                         break;
                 }
            }

            private void clear() {
              ed.setText("");
              tv.setText("");

            }

            private void back() {
             String s=ed.getText().toString();
           if (s!=null){
               s=s.substring(0,s.length()-1);
               ed.setText(s);
               ed.setSelection(s.length());
           }

            }
       /**点击“=”时，记录下操作数2的值，
        * 并将最终运算的式子和结果显示在输出框。
        * 处理输入框，以便进行下一个计算。*/
            private void equal() {
              String s2=ed.getText().toString();
              n2=Double.parseDouble(s2);
              double result=0;
              if (op.equals("+")){
                  result=n1+n2;
              }else if (op.equals("-")){
                  result=n1-n2;
              }else if (op.equals("*")){
                  result=n1*n2;
              }else if (op.equals("/")){
                  result=n1/n2;
              }
              String s=tv.getText().toString();
              tv.setText(s+n2+"="+result);
              ed.setText(result+"");
            }

            private void operator(View v) {
              String s1=ed.getText().toString();
              n1=Double.parseDouble(s1);
              Button button= (Button) v;
             op=button.getText().toString();

              tv.setText(n1+op);
              ed.setText("");
            }

          /**（2）点击小数点“.”时，要判断如果已经有小数点了，就不做任何操作，没有小数点的话，将数字加上小数点。*/
            private void point() {
             String s=ed.getText().toString();
             if (!s.contains(".")){
                 ed.setText(s+".");
                 ed.setSelection(s.length()+1);
             }
            }
           /**（1）点击0到9数字按钮时，输入框内显示值为：原来的值加上新点击的数字，注意要将光标定位在最后。*/
            private void number(View v) {
                Button button=(Button) v;
                String s=ed.getText().toString()+button.getText().toString();
                ed.setText(s);
                ed.setSelection(s.length()); //光标定位在最后
            }
        };
        btn_0.setOnClickListener(click);
        btn_1.setOnClickListener(click);
        btn_2.setOnClickListener(click);
        btn_3.setOnClickListener(click);
        btn_4.setOnClickListener(click);
        btn_5.setOnClickListener(click);
        btn_6.setOnClickListener(click);
        btn_7.setOnClickListener(click);
        btn_8.setOnClickListener(click);
        btn_9.setOnClickListener(click);
        btn_back.setOnClickListener(click);
        btn_clear.setOnClickListener(click);
        btn_add.setOnClickListener(click);
        btn_sub.setOnClickListener(click);
        btn_mul.setOnClickListener(click);
        btn_div.setOnClickListener(click);
        btn_point.setOnClickListener(click);
        btn_equal.setOnClickListener(click);

    }
    private void init() {
        ed = findViewById(R.id.ch2_calc_ed);
        tv = findViewById(R.id.ch2_calc_tv2);
        btn_0 = findViewById(R.id.ch2_calc_btn_0);
        btn_1 = findViewById(R.id.ch2_calc_btn_1);
        btn_2 = findViewById(R.id.ch2_calc_btn_2);
        btn_3 = findViewById(R.id.ch2_calc_btn_3);
        btn_4 = findViewById(R.id.ch2_calc_btn_4);
        btn_5 = findViewById(R.id.ch2_calc_btn_5);
        btn_6 = findViewById(R.id.ch2_calc_btn_6);
        btn_7 = findViewById(R.id.ch2_calc_btn_7);
        btn_8 = findViewById(R.id.ch2_calc_btn_8);
        btn_9 = findViewById(R.id.ch2_calc_btn_9);
        btn_back = findViewById(R.id.ch2_calc_btn_back);
        btn_clear = findViewById(R.id.ch2_calc_btn_clear);
        btn_add = findViewById(R.id.ch2_calc_btn_add);
        btn_sub = findViewById(R.id.ch2_calc_btn_sub);
        btn_mul = findViewById(R.id.ch2_calc_btn_mul);
        btn_div = findViewById(R.id.ch2_calc_btn_div);
        btn_point = findViewById(R.id.ch2_calc_btn_point);
        btn_equal = findViewById(R.id.ch2_calc_btn_equal);
    }
}
